package com.gjintel.framework.dataset.controller;



import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.gjintel.framework.cache.InitCacheData;
import com.gjintel.framework.constant.Constant;
import com.gjintel.framework.dataset.model.DataSet;
import com.gjintel.framework.dataset.service.IDataSetService;
import com.gjintel.framework.log.model.LogInfo;
import com.gjintel.framework.log.util.LogUtil;
import com.gjintel.framework.log.util.LogUtil.LogType;
import com.gjintel.framework.res.model.Resources;
import com.gjintel.framework.user.model.User;
import com.gjintel.framework.util.CommUtil;
import com.gjintel.framework.util.FileResUtil;
import com.gjintel.sdk.util.StringUtil;

import net.sf.json.JSONObject;

@RequestMapping("/framework/dataset")
@Controller
public class DataSetController {
	@Autowired
	private IDataSetService dataSetService;

	/**
	 * @description 对系统信息进行修改操作
	 * @author cxj
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/input")
	public ModelAndView input(HttpSession session) {
		ModelAndView mv = new ModelAndView("/framework/dataset/dataset");		
		DataSet dataSet = dataSetService.getDataSet();
		if (dataSet == null) {		
			dataSet = new DataSet();
		}
		mv.addObject("dataSet", dataSet);
		return mv;
	}

	/**
	 * @方法名：save
	 * @功能说明：保存记录数据
	 * @author cxj
	 */
	@RequestMapping("/save")
	@ResponseBody
	public String save(HttpSession session, DataSet dataSet) {
		User user = (User) session.getAttribute("loginUser");
		boolean sign = false;
		if (StringUtil.isEmpty(dataSet.getId())) {
			sign = true;
		}
		this.dataSetService.saveDataSet(dataSet);
		if (sign) {
			LogUtil.addLog(new LogInfo("添加基础数据配置", user.getId(), user.getName(), String.valueOf(LogType.ADD.ordinal()), dataSet.getClass().getSimpleName(), dataSet.getId()));
		} else {
			LogUtil.addLog(new LogInfo("更新基础数据配置", user.getId(), user.getName(), String.valueOf(LogType.UPDATE.ordinal()), dataSet.getClass().getSimpleName(), dataSet.getId()));
		}
//		WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
//		ServletContext servletContext = webApplicationContext.getServletContext();
//		InitCacheData.getInstance().initGlobalData(dataSet);
//		InitCacheData.getInstance().initWebInfo(servletContext, dataSet);
//		InitCacheData.getInstance().initGoodsTypeData(servletContext);
		return Constant.SAVE_SUCCESS;
	}


	
	/**
	 * 保存文件
	 * @param request
	 * @param response
	 * @param functionFlog
	 * @throws IOException
	 */
	@RequestMapping("/upload")
	@ResponseBody
	public void upload(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		PrintWriter out = response.getWriter();
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		// 取得request中的文件,获取一个list集合
		MultipartFile file = multipartRequest.getFile("Filedata");
		// 得到文件的相对路径
		String relativePath = File.separator + "dataset";
		String uploadPath = request.getSession().getServletContext().getRealPath("file_resource") + relativePath;
		File filePath = new File(uploadPath);
		// 判断文件夹是否存在，如果不存在给与创建出来
		if (!filePath.exists()) {
			filePath.mkdirs();
		}
		String fileName = file.getOriginalFilename();
		Resources res = new Resources();
		String exeName = fileName.substring(fileName.lastIndexOf("."), fileName.length());
		res.setExeName(exeName);
		res.setFilePath(relativePath);
		res.setFileSize(file.getSize());
		res.setOldName(fileName);
		Resources returnRes = FileResUtil.addFileRes(res); // 保存文件记录
		File newFile = new File(uploadPath, res.getId() + res.getExeName());
		file.transferTo(newFile);
		
		//资源文件信息
		out.write(JSONObject.fromObject(returnRes).toString());
		out.flush();
		out.close();
	}


}
